package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.mapper.HUserMapper;
import com.example.mybatisplus.model.domain.User;
import com.example.mybatisplus.model.dto.PageDto;
import org.apache.catalina.Session;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.HUserService;
import com.example.mybatisplus.model.domain.HUser;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 前端控制器
 *
 * @author yfy
 * @version v1.0
 * @since 2021-12-30
 */
@RestController
@RequestMapping("/api/hUser")
public class HUserController {

    private final Logger logger = LoggerFactory.getLogger(HUserController.class);


    @Autowired
    private HUserService userService;
    @Autowired
    private HUserMapper userMapper;

    @GetMapping("/list")
    public JsonResponse list(PageDto page, HUser user) {
        Page<HUser> list = userService.listByPage(page, user);
        return JsonResponse.success(list);
    }

    @GetMapping("/deleteById")
    public JsonResponse deleteById(Long id) {
        userService.removeById(id);
        return JsonResponse.success(true);
    }

    @PostMapping("/updateById")
    public JsonResponse updateById(@RequestBody HUser user) {
        userService.updateById(user);
        return JsonResponse.success(true);
    }

    @PostMapping("/check")
    public JsonResponse checkUser(@RequestBody HUser user) {
        HUser login = userService.login(user);
        System.out.println(login);
        if (login != null) {
            System.out.println(login);
            SessionUtils.saveLoginHUer(login);
            return JsonResponse.success(login, "登陆成功");

        } else return JsonResponse.success(login, "账号密码错误");
    }

    @GetMapping("/showAll")
    public JsonResponse showAll() {
        List<String> users = userMapper.selectAllName();
        return JsonResponse.success(users);
    }

    @GetMapping("/register")
    public JsonResponse register(HUser user) {
        userMapper.insert(user.setUserType(2));
        return JsonResponse.success(user);
    }

    @GetMapping("/loginout")
    public JsonResponse loginout() {
        SessionUtils.clear();
        return JsonResponse.success(true);
    }
}

